package com.tcl.aota.admin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.tcl.aota.persistent.model.*;
import com.tcl.aota.persistent.model.Package;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tcl.aota.admin.dto.PackageDTO;
import com.tcl.aota.admin.dto.Result;
import com.tcl.aota.common.constants.Constants;
import com.tcl.aota.manage.PackageManage;

@Controller
public class PackageController {

	@Resource(name = "packageManage")
	private PackageManage packageManage;

    /**
     * 分页查询发布包
     * @param packageDTO：查询条件
     * @param pageNo：页码
     * @return:json
     */
	@ResponseBody
	@RequestMapping(value = "/package/{pageNo}", method = RequestMethod.POST)
	public Result packageList(@RequestBody PackageDTO packageDTO,@PathVariable Integer pageNo) {
      Map<String, Object> paramMap = packageDTO.toMap();
      paramMap.put("start", (pageNo - 1) * Constants.Common.PAGE_SIZE);
      paramMap.put("pageSize", Constants.Common.PAGE_SIZE);
      int total = packageManage.countByCondition(paramMap);
      List<Package> packageList = packageManage.queryByCondition(paramMap);
      Map<String, Object> resultMap = new HashMap<>();
      resultMap.put("list", packageList);
      resultMap.put("total", total);
      resultMap.put("pageSize", Constants.Common.PAGE_SIZE);
      resultMap.put("pageNo", pageNo);
      return Result.get(resultMap, Constants.Common.SUCCESS);
	}

	@ResponseBody
	@RequestMapping(value = "/package/release/{id}", method = RequestMethod.PUT)
	public Result release(@PathVariable long id) {
		return Result.get(Constants.Common.NULL, packageManage.release(id));
	}

	@ResponseBody
	@RequestMapping(value = "/package/dismiss", method = RequestMethod.POST)
	public Result dismiss(@RequestBody ArrayList<Long> ids) {
		return Result.get(Constants.Common.NULL, packageManage.dismiss(ids));
	}
	
	@ResponseBody
	@RequestMapping(value = "/package/apps/{packageId}", method = RequestMethod.GET)
	public Result appListById(@PathVariable long packageId) {
		return Result.get(packageManage.getAppsByPackage(packageId), Constants.Common.SUCCESS);
	}
}
